package com.glacier.yuyuan.gen.webapp.service.impl;

import cn.hutool.db.meta.MetaUtil;
import cn.hutool.db.meta.Table;
import com.glacier.common.support.service.impl.BaseServiceImpl;
import com.glacier.yuyuan.gen.webapp.domain.GenDatasource;
import com.glacier.yuyuan.gen.webapp.domain.GenFieldType;
import com.glacier.yuyuan.gen.webapp.domain.GenTableField;
import com.glacier.yuyuan.gen.webapp.domain.GenTableInfo;
import com.glacier.yuyuan.gen.webapp.mapper.GenDatasourceMapper;
import com.glacier.yuyuan.gen.webapp.mapper.GenTableInfoMapper;
import com.glacier.yuyuan.gen.webapp.service.DataSourceService;
import com.glacier.yuyuan.gen.webapp.service.GenFieldTypeService;
import com.glacier.yuyuan.gen.webapp.service.GenTableFieldService;
import com.glacier.yuyuan.gen.webapp.service.GenTableInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * date 2023-01-31 16:28
 * 表信息 持久层
 *
 * @author glacier
 * @version 1.0
 */
@Transactional(readOnly = true)
@Service
public class GenTableInfoServiceImpl extends BaseServiceImpl<GenTableInfoMapper, GenTableInfo>
    implements GenTableInfoService {

    private GenTableFieldService genTableFieldService;

    private GenDatasourceMapper genDatasourceMapper;

    private DataSourceService dataSourceService;

    private GenFieldTypeService genFieldTypeService;

    public GenTableInfoServiceImpl() {
        super(GenTableInfoMapper.class);
    }

    @Autowired
    public void setGenTableFieldService(GenTableFieldService genTableFieldService) {
        this.genTableFieldService = genTableFieldService;
    }

    @Autowired
    public void setGenDatasourceMapper(GenDatasourceMapper genDatasourceMapper) {
        this.genDatasourceMapper = genDatasourceMapper;
    }

    @Autowired
    public void setDataSourceService(DataSourceService dataSourceService) {
        this.dataSourceService = dataSourceService;
    }

    @Autowired
    public void setGenFieldTypeService(GenFieldTypeService genFieldTypeService) {
        this.genFieldTypeService = genFieldTypeService;
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @Override
    public <ID extends Serializable> GenTableInfo findById(ID id) {
        GenTableInfo tableInfo = super.findById(id);
        // 查询表字段
        tableInfo.setGenTableFields(genTableFieldService.findByTableId(id));
        return tableInfo;
    }

    /**
     * 获取表字段信息
     *
     * @param sourceId
     * @param tableName
     * @return
     */
    @Override
    public GenTableInfo getGenTableInfo(String sourceId, String tableName) {
        Table table = MetaUtil.getTableMeta(dataSourceService.getDataSource(sourceId), tableName);
        GenTableInfo genTableInfo = new GenTableInfo();
        genTableInfo.setSourceId(sourceId);
        // 查询数据源
        GenDatasource genDatasource = genDatasourceMapper.findById(sourceId);
        genTableInfo.setSourceName(genDatasource.getName());
        // 查询字段映射
        Map<String, GenFieldType> fieldTypeMap = genFieldTypeService.findByDbType(genDatasource.getDbType())
            .stream()
            .collect(Collectors.toMap(GenFieldType::getColumnType, type -> type));
        return genTableInfo.autoFill(table, fieldTypeMap);
    }

    /**
     * 保存
     *
     * @param entity
     * @return
     */
    @Override
    public int save(GenTableInfo entity) {
        int update = super.save(entity);
        List<GenTableField> genTableFields = entity.getGenTableFields();
        if (genTableFields != null && !genTableFields.isEmpty()) {
            for (GenTableField genTableField : genTableFields) {
                genTableField.setTableId(entity.getId());
                genTableFieldService.save(genTableField);
            }
        }
        return update;
    }

    /**
     * 保存并生成代码
     *
     * @param genTableInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveAndGenCode(GenTableInfo genTableInfo) {
        int result = this.save(genTableInfo);
        genCode(genTableInfo);
        return result;
    }

    /**
     * 代码生成
     *
     * @param genTableInfo
     */
    @Override
    public void genCode(GenTableInfo genTableInfo) {

    }
}
